#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : regression.py
# @Author: Richard Chiming Xu
# @Date  : 2021/11/28
# @Desc  : 基于lgb回归的共享单车demo

import pandas as pd
from sklearn.model_selection import train_test_split
import lightgbm as lgb

# temp1：体感温度
# temp2：大气温度
# humidity：湿度
# wind_speed：风速
# holiday：是否为节假，1 是 / 0 否
# weekend：是否为周末，1 是/0否
# season： 季节， 0表示春天 ; 1表示夏天; 2表示秋天; 3-表示冬天。
# weather列说明:
# 1 = 大部分晴朗，少雾/霾
# 2 = 疏云/少云
# 3 = 晴间多云
# 4 = 多云
# 7 = 下雨天/毛毛细雨/小雨
# 10 = 雷雨天气
# 26 = 雪天
# 94 = 冰雾天气

'''
    读取数据
'''
train_data = pd.read_csv('../data/bycycle/train_15.csv')
train_data.dropna(inplace=True)

test_data = pd.read_csv('../data/bycycle/feature_16.csv')
test_data.dropna(inplace=True)

'''
    数据预处理
'''

# 异常季节转换
train_data['season'][train_data['season']==6] = 3
train_data['season'][train_data['season']==5] = 1


# 基于时间求diff
train_data['timestamp'] = pd.to_datetime(train_data['timestamp'])
# 获取月、日、时
train_data['month'] = train_data['timestamp'].dt.month
train_data['day'] = train_data['timestamp'].dt.day
train_data['hour'] = train_data['timestamp'].dt.day
# 求日的diff
train_data['diff_day'] = (train_data['timestamp'] - train_data['timestamp'].min()).dt.days

# 对体感温度与空气问题求集合平均
train_data['temp'] = (train_data['temp1']**2 + train_data['temp2']**2)**0.5

# # onehot 天气与季节
train_data['season'] = train_data['season'].astype('int')
train_data['weather'] = train_data['weather'].astype('int')
train_data[['season_orig','weather_orig']]=train_data[['season','weather']]
train_data=pd.get_dummies(train_data,columns=['season','weather'])

# 基于时间求diff
test_data['timestamp'] = pd.to_datetime(test_data['timestamp'])
# 获取月、日、时
test_data['month'] = test_data['timestamp'].dt.month
test_data['day'] = test_data['timestamp'].dt.day
test_data['hour'] = test_data['timestamp'].dt.day
# 求日的diff
test_data['diff_day'] = (test_data['timestamp'] - test_data['timestamp'].min()).dt.days

# 对体感温度与空气问题求集合平均
test_data['temp'] = (test_data['temp1']**2 + test_data['temp2']**2)**0.5

# # onehot 天气与季节
test_data['season'] = test_data['season'].astype('int')
test_data['weather'] = test_data['weather'].astype('int')
test_data[['season_orig','weather_orig']]=test_data[['season','weather']]
test_data=pd.get_dummies(test_data,columns=['season','weather'])


'''
    模型
'''
cols = ['humidity', 'wind_speed', 'holiday',
       'weekend', 'month', 'day', 'hour', 'diff_day', 'temp', 'season_orig',
       'weather_orig', 'season_0', 'season_1', 'season_2', 'season_3',
       'weather_1', 'weather_2', 'weather_3', 'weather_4', 'weather_7',
       'weather_10', 'weather_26']

lgb_model = lgb.LGBMRegressor(num_leaves=512,
                          max_depth=10,
                          learning_rate=0.005,
                          n_estimators=5000,
                          subsample=0.8,
                          feature_fraction=0.8,
                          reg_alpha=0.5,
                          reg_lambda=0.5,
                          random_state=2021,
                          metric='auc',
                          boosting_type='gbdt',
                          subsample_freq=1,
                          bagging_fraction=0.8)

X = train_data[cols].values
y = train_data['count'].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2021)  # 随机选择25%作为测试集，剩余作为训练集

lgb_reg = lgb_model.fit(X, y)
print('lgb 回归测试分数：{}'.format(lgb_reg.score(X_test, y_test)))
